test code refactoring (#318)
authorSteven G. Johnson <stevenj@alum.mit.edu>
Sat, 22 Nov 2025 14:40:53 +0000 (09:40 -0500)
committerGitHub <noreply@github.com>
Sat, 22 Nov 2025 14:40:53 +0000 (09:40 -0500)
test/custom.c
test/misc.c
test/tests.c
test/tests.h

index fe4239d91b4d1077ba84f58b07a4373ce514aba6..c06bfc61aa19346b3d2de94e7a25bd8e851119ba 100644 (file)
@@ -19,10 +19,7 @@ int main(void)
     utf8proc_uint8_t *output;
     utf8proc_map_custom(input, 0, &output, UTF8PROC_CASEFOLD | UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_NULLTERM,
                         custom, &thunk_test);
-    printf("mapped \"%s\" -> \"%s\"\n", (char*)input, (char*)output);
-    check(strlen((char*) output) == 6, "incorrect output length");
-    check(!memcmp(correct, output, 7), "incorrect output data");
-    free(output);
+    check_compare("map_custom", input, correct, output, 1);
     printf("map_custom tests SUCCEEDED.\n");
     return 0;
 }
index 9156f95541d40e9d5f81e4a085980b31034d078b..bff793dea33e787407c111f936f652b0ace15578 100644 (file)
@@ -7,35 +7,22 @@ static void issue128(void) /* #128 */
     utf8proc_uint8_t input[] = {0x72, 0xcc, 0x87, 0xcc, 0xa3, 0x00}; /* "r\u0307\u0323" */
     utf8proc_uint8_t nfc[] = {0xe1, 0xb9, 0x9b, 0xcc, 0x87, 0x00}; /* "\u1E5B\u0307" */
     utf8proc_uint8_t nfd[] = {0x72, 0xcc, 0xa3, 0xcc, 0x87, 0x00}; /* "r\u0323\u0307" */
-    utf8proc_uint8_t *nfc_out, *nfd_out;
-    nfc_out = utf8proc_NFC(input);
-    printf("NFC \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)nfc_out, (char*)nfc);
-    check(strlen((char*) nfc_out) == 5, "incorrect nfc length");
-    check(!memcmp(nfc, nfc_out, 6), "incorrect nfc data");
-    nfd_out = utf8proc_NFD(input);
-    printf("NFD \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)nfd_out, (char*)nfd);
-    check(strlen((char*) nfd_out) == 5, "incorrect nfd length");
-    check(!memcmp(nfd, nfd_out, 6), "incorrect nfd data");
-    free(nfd_out); free(nfc_out);
+
+    check_compare("NFC", input, nfc, utf8proc_NFC(input), 1);
+    check_compare("NFD", input, nfd, utf8proc_NFD(input), 1);
 }
 
-static void issue102(void) /* #128 */
+static void issue102(void) /* #102 */
 {
     utf8proc_uint8_t input[] = {0x58, 0xe2, 0x81, 0xa5, 0x45, 0xcc, 0x80, 0xc2, 0xad, 0xe1, 0xb4, 0xac, 0x00}; /* "X\u2065E\u0300\u00ad\u1d2c" */
     utf8proc_uint8_t stripna[] = {0x78, 0xc3, 0xa8, 0x61, 0x00}; /* "x\u00e8a" */
     utf8proc_uint8_t correct[] = {0x78, 0xe2, 0x81, 0xa5, 0xc3, 0xa8, 0x61, 0x00}; /* "x\u2065\u00e8a" */
     utf8proc_uint8_t *output;
+
     utf8proc_map(input, 0, &output, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
         UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_CASEFOLD | UTF8PROC_IGNORE | UTF8PROC_STRIPNA);
-    printf("NFKC_Casefold \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)output, (char*)stripna);
-    check(strlen((char*) output) == 4, "incorrect NFKC_Casefold+stripna length");
-    check(!memcmp(stripna, output, 5), "incorrect NFKC_Casefold+stripna data");
-    free(output);
-    output = utf8proc_NFKC_Casefold(input);
-    printf("NFKC_Casefold \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)output, (char*)correct);
-    check(strlen((char*) output) == 7, "incorrect NFKC_Casefold length");
-    check(!memcmp(correct, output, 8), "incorrect NFKC_Casefold data");
-    free(output);
+    check_compare("NFKC_Casefold+stripna", input, stripna, output, 1);
+    check_compare("NFKC_Casefold", input, correct, utf8proc_NFKC_Casefold(input), 1);
 }
 
 int main(void)
index 147ac39931202306e17e07692c649b79dee83c62..8a47b85a2f6a5c0d060052a8bc2713957b590826 100644 (file)
@@ -8,7 +8,10 @@ void check(int cond, const char *format, ...)
 {
      if (!cond) {
           va_list args;
-          fprintf(stderr, "line %zd: ", lineno);
+          if (lineno)
+               fprintf(stderr, "FAILED at line %zd: ", lineno);
+          else
+               fprintf(stderr, "FAILED: ");
           va_start(args, format);
           vfprintf(stderr, format, args);
           va_end(args);
@@ -58,3 +61,40 @@ size_t simple_getline(unsigned char buf[8192], FILE *f) {
     buf[i] = 0;
     return i;
 }
+
+void print_escaped(FILE* f, const utf8proc_uint8_t *utf8) {
+     fprintf(f, "\"");
+     while (*utf8) {
+          utf8proc_int32_t codepoint;
+          utf8 += utf8proc_iterate(utf8, -1, &codepoint);
+          if (codepoint < 0x10000)
+               fprintf(f, "\\u%04x", codepoint);
+          else
+               fprintf(f, "\\U%06x", codepoint);
+     }
+     fprintf(f, "\"");
+}
+
+void print_string_and_escaped(FILE* f, const utf8proc_uint8_t *utf8) {
+     fprintf(f, "\"%s\" (", (const char *) utf8);
+     print_escaped(f, utf8);
+     fprintf(f, ")");
+}
+
+void check_compare(const char *transformation,
+                   const utf8proc_uint8_t *input, const utf8proc_uint8_t *expected,
+                   utf8proc_uint8_t *received, int free_received) {
+     int passed = !strcmp((const char *) received, (const char *) expected);
+     FILE *f = passed ? stdout : stderr;
+     fprintf(f, "%s: %s ", passed ? "PASSED" : "FAILED", transformation);
+     print_string_and_escaped(f, input);
+     fprintf(f, " -> ");
+     print_string_and_escaped(f, received);
+     if (!passed) {
+          fprintf(f, " != expected ");
+          print_string_and_escaped(f, expected);
+     }
+     fprintf(f, "\n");
+     if (free_received) free(received);
+     if (!passed) exit(1);
+}
index acda3291e7049068259b839a6cf555402df2a2bb..d55779fa064c3e73df3b7a63cc5c5ca81f33f567 100644 (file)
@@ -25,3 +25,8 @@ void check(int cond, const char *format, ...);
 size_t skipspaces(const unsigned char *buf, size_t i);
 size_t encode(unsigned char *dest, size_t *dest_len, const unsigned char *buf);
 size_t simple_getline(unsigned char buf[8192], FILE *f);
+void print_escaped(FILE* f, const utf8proc_uint8_t *utf8);
+void print_string_and_escaped(FILE* f, const utf8proc_uint8_t *utf8);
+void check_compare(const char *transformation,
+                   const utf8proc_uint8_t *input, const utf8proc_uint8_t *expected,
+                   utf8proc_uint8_t *received, int free_received);